package library.and.xps.com.step.view.fragment;

import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jude.utils.JUtils;
import com.squareup.okhttp.Request;

import library.and.xps.com.step.R;
import library.and.xps.com.step.URLConstants;
import library.and.xps.com.step.adapter.TeamRankingAdapter;
import library.and.xps.com.step.m.model.TeamRankingRes;
import library.and.xps.com.step.m.netapi.OkHttpClientUtils;
import library.and.xps.com.step.net.ResponceWrapper;
import library.and.xps.com.step.util.AnimUtil;
import library.and.xps.com.step.util.CommonUtils;
import library.and.xps.com.step.util.HttpResponsHelper;
import library.and.xps.com.step.util.RequestCallback;
import library.and.xps.com.step.util.TeamHelper;
import library.and.xps.com.step.view.base.BaseFragment;

public class TeamRankingFragment extends BaseFragment {
    TextView tv_champion_team_name, tv_champion_team_desc, tv_team_ranking, tv_team_ave_step;
    ListView lv_team_ranking;
    TeamRankingAdapter teamRankingAdapter;

    @Override
    protected int getRes() {
        return R.layout.fragment_team_ranking;
    }

    @Override
    protected void init(View view) {
        tv_champion_team_name = r2v(view, R.id.tv_champion_team_name);
        tv_champion_team_desc = r2v(view, R.id.tv_champion_team_desc);
        tv_team_ranking = r2v(view, R.id.tv_team_ranking);
        tv_team_ave_step = r2v(view, R.id.tv_team_ave_step);
        lv_team_ranking = r2v(view, R.id.lv_team_ranking);
    }

    @Override
    protected void setData(View view) {
        teamRankingAdapter = new TeamRankingAdapter(getActivity());
        lv_team_ranking.setAdapter(teamRankingAdapter);

    }

    @Override
    public void onResume() {
        super.onResume();
        if (!TeamHelper.getIns().hasTeam()) return;
        request();
    }

    void initCurrentTeamInfoTab(TeamRankingRes teamRankingRes) {
        tv_team_ave_step.setText(teamRankingRes.getObj().getWalk() + "");

        int teamRanking = 0;
        for (TeamRankingRes.ListBean s : teamRankingRes.getList()) {
            ++teamRanking;
            if (TeamHelper.getIns().getTid().equals(s.getTeamid() + "")) {
                tv_team_ranking.setText(teamRanking + "");
                break;
            }
        }
    }

    void request() {
        String data = URLConstants.TEAM.GetEnterpriseRanking + "?teamid=" + TeamHelper.getIns().getCurT().getTeamid();
        OkHttpClientUtils.getDataAsync(getActivity(), data, new RequestCallback() {
            @Override
            public void onFailure(Request request, Exception e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(ResponceWrapper response) {
                if (HttpResponsHelper.SuccessResponce(response)) {
                    JUtils.Log("TeamRankingFragment",response.getBody());
                    TeamRankingRes teamRankingRes = new Gson().fromJson(response.getBody(), TeamRankingRes.class);
                    if(teamRankingRes==null||teamRankingRes.getObj()==null)return;
                    teamRankingAdapter.setDataset(teamRankingRes.getList());
                    initCurrentTeamInfoTab(teamRankingRes);
                    initChampionTeam(teamRankingRes);
                    CommonUtils.resetListViewHeight(lv_team_ranking);
                }
            }

            @Override
            public void onStart() {

            }
        }, "");
    }

    void initChampionTeam(TeamRankingRes teamRankingRes) {
        if (teamRankingRes == null || teamRankingRes.getList().size() <= 0) return;
         TeamRankingRes.ListBean listBean =  teamRankingRes.getList().get(0);
        tv_champion_team_name.setText(listBean.getEnterprise());
        tv_champion_team_desc.setText(listBean.getDescription());
    }

    @Override
    public void onClick(View v) {

    }
}
